package com.vinjogames.ui;

import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import com.vinjogames.util.VertexBuffer;

public class Grid {

	private FloatBuffer vertexBuffer;
	private float vertices[];

	public Grid(int width, int height){

		vertices = new float[(width+1)*4*3];
		//Draws the grid by passing 3 floats per point
		//12 = 4 points w/3 floats each
		for(int i = 0; i < vertices.length; i++){
			switch(i % 12){
			case 1:
			case 4:
				vertices[i] = (i / 12) > height ? height : i /12;
				break;
			case 6:
			case 9:
				vertices[i] = (i / 12);
				break;
			case 3:
				vertices[i] = width;
				break;
			case 10:
				vertices[i] = height;
				break;
			default:
				vertices[i] = 0;
				break;
			}
		}
		
		vertexBuffer = VertexBuffer.getVertexBuffer(vertices);

	}

	public void draw(GL10 gl){
		// Temporarily disable blending to see the grid.
		gl.glDisable(GL10.GL_BLEND);

		// Point to Buffers
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

		// Set Face
		gl.glFrontFace(GL10.GL_CW);

		// Color (1 is max)  (Don't do this with textures
		gl.glColor4f(0f, 0f, 0f, 1f);

		// Point to buffer
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glLineWidthx(5);
		gl.glDrawArrays(GL10.GL_LINES, 0, vertices.length / 3);
	
		// Disable Client State before Leaving
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

		// Re-establish Blending
		gl.glEnable(GL10.GL_BLEND);
		gl.glColor4f(1,1,1,1);
	}
}